<?php
namespace Frontend\Controllers;

use Models\Vote;
use Models\VoteOption;
use Models\VoteUser;
use Models\Wx;

class VoteController extends BaseController
{
    public function indexAction()
    {
        $token = $this->request->get('token');
        $this->view->setVar('token',$token);
        $Wx=Wx::findFirstByToken($token);
        $this->view->setVar('Wx', $Wx);
        $host = $this->config->host;
        $this->view->setVar('host',$host);
        $votes = Vote::findByToken($token);
        if (!$votes) {
            return $this->flash->warning("你访问的页面不存在");
        }
        $votesArr = $votes->toArray();

        for ($i = 0; $i < count($votesArr); ++$i) {
            $count = 0;
            $voteoption = VoteOption::findByVid($votesArr[$i]['id']);
            $voteoptionArr = $voteoption->toArray();
            for ($j = 0; $j < count($voteoptionArr); ++$j) {
                $count += $voteoptionArr[$j]['count'];
            }
            $votesArr[$i]['count'] = $count;
        }
        $currentPage = $this->request->getQuery('page');
        $paginator = new \Phalcon\Paginator\Adapter\NativeArray(array(
            "data" => $votesArr,
            "limit" => 10,
            "page" => $currentPage,
        ));
        $this->view->page = $paginator->getPaginate();

    }

    public function  detailAction()
    {
        $open_id = $this->request->get('openid');
        $ip = $this->request->getClientAddress();
        $id = $this->request->get('id');
        $vote = Vote::findFirst($id);
        $this->view->setVar('vote', $vote);
        $token=$this->request->get('token');
        $Wx=Wx::findFirstByToken($token);
        $this->view->setVar('Wx', $Wx);
        $voteOptions = VoteOption::findByVid($id);
        $this->view->setVar('voteOptions', $voteOptions);
        if ($vote->time_end < date("Y-m-d")){
            $this->view->setVar("is_ok", 1);
        }
        else {
            $this->view->setVar("is_ok", 0);
        }
        $count = 0;
        $voteoption = VoteOption::findByVid($id);
        $voteoptionArr = $voteoption->toArray();

        for ($j = 0; $j < count($voteoptionArr); ++$j) {
            $count += $voteoptionArr[$j]['count'];
        }
        $this->view->setVar('count', $count);
        if ($vote->type == 1) {//单选
            $hasuser = 0;
            if ($open_id) {
                $voteuser = VoteUser::find("open_id='" . $open_id . "' AND ip='".$ip."' AND vid='" . $id . "'");
                if ($voteuser->toArray()) {
                    $hasuser = 1;
                }
            } else {
                $voteuser = VoteUser::find("ip='" . $ip . "' AND vid='" . $id . "'");
                if ($voteuser->toArray()) {
                    $hasuser = 1;
                }
            }
            if ($hasuser == 1) return $this->flash->warning("您已经投过票了");
            if ($post = $this->request->getPost('selected')) {
                $votechange = VoteOption::findFirstByid($post);
                $votechange->count += 1;
                if ($votechange->save()) {
                    $voteuser = new VoteUser();
                    $voteuser->vid = $id;
                    $voteuser->open_id = $open_id;
                    $voteuser->ip = $this->request->getClientAddress();
                    $voteuser->time = date("Y-m-d H:i:s", strtotime("now"));
                    $voteuser->save();
                    return $this->flash->warning("投票成功");
                } else {
                    return $this->flash->warning("投票失败");
                }
            }
        } else  //多选
        {
            $hasuser = 0;
            if ($open_id) {
                $voteuser = VoteUser::find("open_id='" . $open_id . "' AND ip='".$ip."' AND vid='" . $id . "'");
                if ($voteuser->toArray()) {
                    $hasuser = 1;
                }
            } else {
                $voteuser = VoteUser::find("ip='" . $ip . "' AND vid='" . $id . "'");
                if ($voteuser->toArray()) {
                    $hasuser = 1;
                }
            }
            if ($hasuser == 1) return $this->flash->warning("您已经投过票了");
            if ($post = $this->request->getPost()) {
                for ($i = 0; $i < count($post['select']); ++$i) {
                    $votechanges = VoteOption::findFirst($post['select'][$i]);
                    $votechanges->count += 1;
                    $votechanges->save();
//                    if ($votechanges->save()) {
//                        return $this->flash->warning("投票成功");
//                    } else {
//                        return $this->flash->warning("投票失败");
//                    }
                }
                $voteuser = new VoteUser();
                $voteuser->vid = $id;
                $voteuser->open_id = $open_id;
                $voteuser->ip = $this->request->getClientAddress();
                $voteuser->time = date("Y-m-d H:i:s", strtotime("now"));
                $voteuser->save();
                return $this->flash->warning("投票成功");
            }

        }

    }
}

